package edu.nus.mtech.sem1.ca.main;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import edu.nus.mtech.sem1.ca.cust.Customer;
import edu.nus.mtech.sem1.ca.cust.complaint.Complaint;
import edu.nus.mtech.sem1.ca.cust.complaint.ComplaintStatus;
import edu.nus.mtech.sem1.ca.exception.CustomerNotFoundException;
import edu.nus.mtech.sem1.ca.main.dao.TelephonyApplicationDao;
import edu.nus.mtech.sem1.ca.service.Service;
import edu.nus.mtech.sem1.ca.utils.Common;
import edu.nus.mtech.sem1.ca.utils.DateInputControl;
import edu.nus.mtech.sem1.ca.utils.Logger;
import edu.nus.mtech.sem1.ca.utils.TelephonyConstants;
import edu.nus.mtech.sem1.ca.utils.TelephonyUtils;


/**
 * Single page for complaint
 *
 */
public class FormComplaint extends javax.swing.JInternalFrame {
	private JLabel AccNo;
	private JTextField txtComplaintNo;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JTextPane txtDescription;
	private JLabel jLabel6;
	private DateInputControl txtComplaintdate;
	private JTextField txtAccountNo;
	private JTextField txtNRIC;
	private JLabel jLabel1;
	private Complaint mComplaint=new Complaint();
	private TelephonyApplicationDao mDao=new TelephonyApplicationDao();
	private JCheckBox chkStatus;
	private JComboBox cboService;
	private JButton btnComplaintsButton;
	
	private String nric;
	private String name;

	
	/*public static void main(String[] args) {
		JFrame frame = new JFrame();
		FormComplaint inst = new FormComplaint();
		JDesktopPane jdp = new JDesktopPane();
		jdp.add(inst);
		jdp.setPreferredSize(inst.getPreferredSize());
		frame.setContentPane(jdp);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}*/
	
	/**
	 * Calling when viewing a complaint from Complaints list
	 * @param nric
	 * @param complaintNo
	 * @param name
	 */
	public FormComplaint(String nric, String complaintNo, String name) {
		super();
		this.nric=nric;
		this.name=name;
		initGUI();
		this.setTitle("Detailed information of Complaint no "+complaintNo +" from "+name);
		Complaint returnComplaint=getComplaintForCustomerFromComplaintNumber(nric, complaintNo);
		mComplaint=returnComplaint;
		bindControls(returnComplaint);
	}
	
	/**
	 * Called when a new complaint is created 
	 * @param customer
	 * @throws CustomerNotFoundException 
	 */
	public FormComplaint(String nric, String name) throws CustomerNotFoundException {
		super();
		this.nric=nric;
		this.name=name;
		initGUI();
		this.setTitle("Log new complaint for "+name);
		Customer customerData=null;
		try {
			customerData = mDao.retrieveCustomerData(nric);
		} catch (CustomerNotFoundException e) {
			throw e;
		}
		
		Set<Complaint> returnComplaint=mDao.retrieveComplaintsForCustomer(nric);
		String complaintNo="C0000";
		if (returnComplaint==null){
			complaintNo=complaintNo+"1";
		}
		else{
			complaintNo=complaintNo+(returnComplaint.size()+1);
		}
		
		Complaint complaint=new Complaint();
		complaint.setAccountNo(customerData.getAccountNo());
		complaint.setComplaintDate(Calendar.getInstance().getTime());
		complaint.setComplaintNo(complaintNo);
		complaint.setNric(nric);
		complaint.setStatus(ComplaintStatus.OPEN);
		bindControls(complaint);
	}
	
	public FormComplaint() {
		super();
		initGUI();
		this.setTitle("Log New Complaint");
		ClearForm();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 400));
			setVisible(true);			
			this.setClosable(true);
			getContentPane().setLayout(null);
			{
				AccNo = new JLabel();
				AccNo.setBounds(20, 89, 100, 20);
				getContentPane().add(AccNo);
				AccNo.setText("Account Number");
				AccNo.setPreferredSize(new java.awt.Dimension(100, 20));
			}
			{
				jLabel1 = new JLabel();
				jLabel1.setBounds(20, 63, 100, 20);
				getContentPane().add(jLabel1);
				jLabel1.setText("NRIC");
				jLabel1.setPreferredSize(new java.awt.Dimension(100,20));
			}
			{
				txtNRIC = new JTextField();
				txtNRIC.addFocusListener(new FocusAdapter() {
					@Override
					public void focusLost(FocusEvent arg0) {
						searchCustomer();
					}
				});
				txtNRIC.setBounds(155, 63, 282, 21);
				getContentPane().add(txtNRIC);
				txtNRIC.setPreferredSize(new java.awt.Dimension(282,21));
			}
			{
				txtAccountNo = new JTextField();
				txtAccountNo.setEditable(false);
				txtAccountNo.setBounds(155, 89, 282, 21);
				getContentPane().add(txtAccountNo);
				txtAccountNo.setPreferredSize(new java.awt.Dimension(282,21));
				txtAccountNo.setAutoscrolls(false);
			}
			{
				jLabel3 = new JLabel();
				jLabel3.setBounds(20, 115, 100, 20);
				getContentPane().add(jLabel3);
				jLabel3.setText("Service");
				jLabel3.setPreferredSize(new java.awt.Dimension(100,20));
			}
			{
				jLabel4 = new JLabel();
				jLabel4.setBounds(20, 141, 100, 20);
				getContentPane().add(jLabel4);
				jLabel4.setText("Complaint Date");
				jLabel4.setPreferredSize(new java.awt.Dimension(100,20));
			}
			{
				txtComplaintdate = new DateInputControl();
				txtComplaintdate.setBounds(155, 141, 282, 21);
				getContentPane().add(txtComplaintdate);
				txtComplaintdate.setPreferredSize(new java.awt.Dimension(282,21));
			}
			{
				jLabel5 = new JLabel();
				jLabel5.setBounds(20, 167, 100, 20);
				getContentPane().add(jLabel5);
				jLabel5.setText("Close Status");
				jLabel5.setPreferredSize(new java.awt.Dimension(100, 20));
			}
			{
				jLabel6 = new JLabel();
				jLabel6.setBounds(20, 199, 150, 20);
				getContentPane().add(jLabel6);
				jLabel6.setText("Complaint Description");
				jLabel6.setPreferredSize(new java.awt.Dimension(150, 20));
			}
			{
				txtDescription = new JTextPane();
				txtDescription.setBounds(155, 199, 282, 163);
				getContentPane().add(txtDescription);
				txtDescription.setPreferredSize(new java.awt.Dimension(312, 82));
			}
			
			chkStatus = new JCheckBox("");
			chkStatus.setBounds(155, 164, 97, 23);
			getContentPane().add(chkStatus);
			
			JButton btnNew = new JButton("New");
			btnNew.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ClearForm();
				}
			});
			btnNew.setBounds(10, 373, 89, 23);
			getContentPane().add(btnNew);
			
			JButton btnSave = new JButton("Save");
			btnSave.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(SaveComplaint()){
						JOptionPane.showMessageDialog(null, TelephonyConstants.MSG_SAVED_SUCCESSFULLY, "Info", JOptionPane.INFORMATION_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(null, TelephonyConstants.MSG_SAVED_FAIL, "Info", JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
			btnSave.setBounds(109, 373, 89, 23);
			getContentPane().add(btnSave);
			
			JButton btnClose = new JButton("Close");
			btnClose.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					setVisible(false);
					try {
						finalize();
					} catch (Throwable e) {
						// TODO: handle exception
						Logger.log(e.getMessage());
					}					
				}
			});
			btnClose.setBounds(208, 373, 89, 23);
			getContentPane().add(btnClose);
			
			JPanel panel = new JPanel();
			panel.setBorder(null);
			panel.setBounds(10, 11, 436, 44);
			getContentPane().add(panel);
			panel.setLayout(null);
			{
				jLabel2 = new JLabel();
				jLabel2.setBounds(10, 11, 100, 20);
				panel.add(jLabel2);
				jLabel2.setText("Complaint No.");
				jLabel2.setPreferredSize(new java.awt.Dimension(100,20));
			}
			{
				txtComplaintNo = new JTextField();
				txtComplaintNo.setEditable(false);
				txtComplaintNo.setBounds(144, 11, 282, 21);
				panel.add(txtComplaintNo);
				txtComplaintNo.setPreferredSize(new java.awt.Dimension(282, 21));
			}
			
			cboService = new JComboBox();
			cboService.setModel(new DefaultComboBoxModel(new String[] {"Digital Voice", "Mobile Voice", "Cable TV"}));
			cboService.setSelectedIndex(0);
			cboService.setBounds(155, 115, 282, 20);
			getContentPane().add(cboService);
			{
				btnComplaintsButton = new JButton("Complaints");
				btnComplaintsButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						FormAllComplaints allComplaints=new FormAllComplaints(nric, name);
						getParent().add(allComplaints);
						Common.OpenCenterScreen(allComplaints, true);
					}
				});
				btnComplaintsButton.setBounds(321, 373, 116, 23);
				getContentPane().add(btnComplaintsButton);
			}
			pack();
			this.setSize(471, 438);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void searchCustomer(){
		try {			
			
			txtAccountNo.setText(mDao.retrieveCustomerData(txtNRIC.getText()).getAccountNo());
		} catch (CustomerNotFoundException e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, e.getMessage(), "Info", JOptionPane.INFORMATION_MESSAGE);
			Logger.log(e.getMessage());
		}catch(Exception e1){
			Logger.log(e1.getMessage());
		}
	}
	
	private Complaint getComplaintForCustomerFromComplaintNumber(String nric, String complaintNo){
		Complaint returnComplaint=null;
		
		try {			
			Set<Complaint> retrieveComplaintsForCustomer = mDao.retrieveComplaintsForCustomer(nric);
			for (Complaint complaint : retrieveComplaintsForCustomer) {
				if (complaint.getComplaintNo()!=null && complaint.getComplaintNo().equalsIgnoreCase(complaintNo)){
					returnComplaint=complaint;
					break;
				}
			}
		
		} catch(Exception e1){
			Logger.log(e1.getMessage());
		}
		return returnComplaint;
	}
	
	private void bindControls(Complaint complaint){
		txtAccountNo.setText(complaint.getAccountNo());
		txtComplaintdate.setText(TelephonyUtils.getDateTimeStringFromDate(complaint.getComplaintDate()));
		txtComplaintNo.setText(complaint.getComplaintNo());
		txtDescription.setText(complaint.getComplaintDesc());
		txtNRIC.setText(complaint.getNric());
		SetServiceType();
		chkStatus.setSelected(complaint.getStatus()==ComplaintStatus.CLOSED);
	}
	
	private void BindObjects(){
		mComplaint.setAccountNo(txtAccountNo.getText().trim());
		mComplaint.setComplaintDate(txtComplaintdate.getDate());
		mComplaint.setComplaintDesc(txtDescription.getText());
		mComplaint.setComplaintNo(GetComplaintNo());
		mComplaint.setNric(txtNRIC.getText());
		mComplaint.setService(GetServiceType());
		mComplaint.setStatus(chkStatus.isSelected()? ComplaintStatus.CLOSED:ComplaintStatus.OPEN);		
	}
	private String GetComplaintNo(){
		if(txtComplaintNo.getText().trim().equals("TBA")){
			//new complaint
			String l_complaintNo= "C-"+txtNRIC.getText().trim()+"_";
			java.util.Calendar l_tmpDate=Calendar.getInstance();
			l_tmpDate.setTime(txtComplaintdate.getDate());
			l_complaintNo+= Integer.toString(l_tmpDate.get(Calendar.YEAR));
			l_complaintNo+= Integer.toString(l_tmpDate.get(Calendar.MONTH));
			l_complaintNo+= Integer.toString(l_tmpDate.get(Calendar.DAY_OF_MONTH));
			l_complaintNo+= Integer.toString(l_tmpDate.get(Calendar.HOUR));
			l_complaintNo+= Integer.toString(l_tmpDate.get(Calendar.MINUTE));
			l_complaintNo+= Integer.toString(l_tmpDate.get(Calendar.SECOND));
			return l_complaintNo;
		}else{
			//for existing complaint
			return txtComplaintNo.getText();
		}
	}
	private Service GetServiceType(){
		switch (cboService.getSelectedIndex()) {
		case 0:
			return Service.DVOICE;
		case 1: 
			return Service.MOBILE;
		case 2: 
			return Service.CABLE;
		default:
			return Service.DVOICE;
		}
	}
	private void SetServiceType(){
		Service l_service = mComplaint.getService();
		if(l_service==Service.DVOICE){
			cboService.setSelectedIndex(0);
		}else if(l_service==Service.MOBILE){
			cboService.setSelectedIndex(1);
		}else if(l_service==Service.CABLE){
			cboService.setSelectedIndex(2);
		}else{
			cboService.setSelectedIndex(-1);
		}
	}
	
	private boolean SaveComplaint(){
		if(IsValid()){
			BindObjects();
			mDao.addOrUpdateComplaintForCustomer(mComplaint.getNric(),mComplaint);
			bindControls(mComplaint);
			return true;
		}
		else{
			return false;
		}
	}
	
	private boolean IsValid(){		
		if(cboService.getSelectedIndex()<0){
			JOptionPane.showMessageDialog(null, TelephonyConstants.MSG_CHOOSE_SERVICE_TYPE, "Info", JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
		if(txtNRIC.getText().trim()==""){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_NRIC,"Info",JOptionPane.INFORMATION_MESSAGE);
		}
		return true;
	}
	private void ClearForm(){
		mComplaint=new Complaint();
		mComplaint.setComplaintNo("TBA");//set for new complaint
		bindControls(mComplaint);
	}
}
